package com.test.platform.controller;

import com.test.platform.model.XTSReport;
import com.test.platform.service.XTSReport.XTSReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/xts-reports")
public class XTSReportController {
    
    @Autowired
    private XTSReportService xtsReportService;
    
    @GetMapping
    public ResponseEntity<List<XTSReport>> getAllXTSReports() {
        List<XTSReport> reports = xtsReportService.getAllXTSReports();
        return ResponseEntity.ok(reports);
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<XTSReport> getXTSReportById(@PathVariable Integer id) {
        XTSReport report = xtsReportService.getXTSReportById(id);
        return report != null ? ResponseEntity.ok(report) : ResponseEntity.notFound().build();
    }
    
    @GetMapping("/search")
    public ResponseEntity<List<XTSReport>> getXTSReportsByOwnerAndName(
            @RequestParam String owner, 
            @RequestParam String name) {
        List<XTSReport> reports = xtsReportService.getXTSReportsByOwnerAndName(owner, name);
        return ResponseEntity.ok(reports);
    }
    
    @PostMapping
    public ResponseEntity<XTSReport> createXTSReport(@RequestBody XTSReport xtsReport) {
        boolean success = xtsReportService.createXTSReport(xtsReport);
        return success ? ResponseEntity.ok(xtsReport) : ResponseEntity.badRequest().build();
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<XTSReport> updateXTSReport(
            @PathVariable Integer id, 
            @RequestBody XTSReport xtsReport) {
        xtsReport.setId(id);
        boolean success = xtsReportService.updateXTSReport(xtsReport);
        return success ? ResponseEntity.ok(xtsReport) : ResponseEntity.notFound().build();
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteXTSReport(@PathVariable Integer id) {
        boolean success = xtsReportService.deleteXTSReport(id);
        return success ? ResponseEntity.ok().build() : ResponseEntity.notFound().build();
    }
}